class Solution {
public:
    bool IsBalanced_Solution(TreeNode* pRoot) {
        int depth=0;
        return IsBalancedCore(pRoot,depth);
    }
    
    bool IsBalancedCore(TreeNode* pRoot,int& depth){
        if(pRoot==nullptr){
            depth=0;
            return true;
        }
        int left,right;
        if(IsBalancedCore(pRoot->left,left)&&IsBalancedCore(pRoot->right,right)){
            int diff=left-right;
            if(diff>=-1&&diff<=1){
                depth=1+(left>right?left:right);
                return true;
            }
        }
        return false;
    }
};